<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_actions_block_ListUsersAction extends m_backoffice_extends_Action
{
	public function execute()
	{	
		$parentDoc = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode("users");
		$superUser = $this->getDocumentProvider()->loadDocuments("modules_users/user")->retrieveFirstDocument();

		if($this->getRequestParameter("docId"))
		{
			$docId = $this->getRequestParameter("docId");
            $parentDoc = $this->getDocumentProvider()->getByModelAndUniqueId("modules_users/group", $docId, $this->getContextLang());
		}

        $listService = m_backoffice_services_ListService::getInstance();

        //Sort and search parameters
        $sortParams = $listService->getSortParams("user", $this->getRequestParameter("column"), $this->getRequestParameter("order"));
        $searchParams = $listService->getSearchParams("user", $this->getRequestParameter("searchValues"));

        //Get the nbDocTot;
        $nbDocTot = $listService->getDocuments($parentDoc, array("modules_users/user"), null, null, array(), $searchParams, true);
        $pagination = $listService->pagination("user", $nbDocTot, $this->getRequestParameter("numPage"), $this->getRequestParameter("nbDoc"), 10);

		$usersData = $listService->getDocuments($parentDoc, array("modules_users/user"), $pagination["nbDoc"], $pagination["numPage"], $sortParams, $searchParams);

        $this->addToModel("showTitle", $this->getBlockParameter("showTitle"));
		$this->addToModel("users", $usersData);
        $this->addToModel("pagination", $pagination);
        $this->addToModel("docSearchParams", $searchParams);
        $this->addToModel("searchParams", $this->getContext("searchParams_user"));
        $this->addToModel("sortParams", $sortParams);
		$this->addToModel("su", $superUser);
		$this->addToModel("currentUser", $this->getUser()->getUniqueId());
		$this->addToModel("group", $parentDoc);
		$this->addToModel("addNewUrl", f_core_Routing::getUrl("addUser", array("docId" => $docId)));
        $this->addToModel('fileLink', $parentDoc->getEditUrl($parentDoc->getLang(),'users'));

		$this->setView('success');
	}	
}